import { writeFile, utils } from 'xlsx'

const getRandomRGBColor = () => {
  const r = Math.floor(Math.random() * 256)
  const g = Math.floor(Math.random() * 256)
  const b = Math.floor(Math.random() * 256)
  return `rgb(${r}, ${g}, ${b})`
}
const generateUniqueRGBColors = (numColors) => {
  const colorSet = new Set()

  while (colorSet.size < numColors) {
    colorSet.add(getRandomRGBColor())
  }

  return Array.from(colorSet)
}

const getCurentTime = () => {
  var now = new Date()
  //获取当前年份
  var year = now.getFullYear()
  //获取当前月份
  var month = now.getMonth() + 1
  //获取当前日期
  var day = now.getDate()
  //获取当前时刻
  var hh = now.getHours()
  //获取当前分钟
  var mm = now.getMinutes()
  //获取当前秒钟
  var ss = now.getSeconds()
  //将当前的日期拼串
  var clock = year + '-'
  if (month < 10) clock += '0'
  clock += month + '-'
  if (day < 10) clock += '0'
  clock += day + '-'
  if (hh < 10) clock += '0'
  clock += hh + ':'
  if (mm < 10) clock += '0'
  clock += mm + ':'
  if (ss < 10) clock += '0'
  clock += ss
  return clock
}

const handleExport = (data = {}, fileName = 'data') => {
  // 将表格数据导出为excel文件
  // 将数据转换为工作表
  const worksheet = utils.json_to_sheet(data)
  // 创建一个工作簿并将工作表添加进去
  const workbook = utils.book_new()
  utils.book_append_sheet(workbook, worksheet, 'Sheet1')

  // 将工作簿导出为 Excel 文件
  writeFile(workbook, `${fileName}.xlsx`)
}

export { getRandomRGBColor, generateUniqueRGBColors, getCurentTime, handleExport }
